// JavaScript Document 
// ORIGINAL FILE: 
// C:\developer\jboss-4.0.1sp1\server\default\deploy\cfusion.war\js\ajax.js
var myNote = '';
var myType = '';
var MYSPEED = 500;
var usermessage_updated_fl = false;
var targetObj = '';	// object element that should be filled with response from iframe

/* AJAX FUNCTIONS */
function ajaxPushPull(url,obj) {	// sends info to the URL, puts the return HTML into the obj.innerHTML
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX");
				return false;
			}
		}
	}
	
	var tmpURL = url;
	
	if(url.indexOf('?') < 0) {
		url = url + '?';
	}
	if(url.indexOf('ajax=1') < 0) {
		url = url + '&ajax=1';
	}
	
	// random number appended to URL b/c IE caches xml response
	var rnd = Math.floor(Math.random() * 100001);
	url = url + '&randomAjaxValue='+rnd;
	
	/*
	readyState property:
	State	Description
	0		The request is not initialized
	1		The request has been set up
	2		The request has been sent
	3		The request is in process
	4		The request is complete
	*/
	try {
		obj.style.opacity = 1;
	} catch(e) {}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==1
		   	|| xmlHttp.readyState==2
			|| xmlHttp.readyState==3) {
			
			if(ge('loading') == undefined) {
				// create the floating loading message
				var loadDiv = document.createElement('div');
				loadDiv.id = 'loading';
				loadDiv.className = 'loading';
				loadDiv.innerHTML = 'loading...<br /><img src="/media/loading.gif" />';
				// put the loading div as close to the object as possible, right before the obj
				try {
					obj.parentNode.insertBefore(loadDiv,obj);
				} catch(e) {}
				/* -- removing fades
				try {
					opacity(obj.id, 99, 0, MYSPEED);	// fade out
				} catch(e) {}
				try {
					if(ge('myResponseText')) {
						opacity('myResponseText', 99, 0, MYSPEED);	// fade out
					}
				} catch(e) {}
				*/
			}
		}
		if(xmlHttp.readyState==4) {
			// if it's ready, then do this...
				// 1. put the results into the INNERHTML of the passed object
			try {
				/* -- removing fades
				if(obj) {
					changeOpac(0, obj.id);	// set opacity to 0
				}
				*/
				obj.innerHTML = xmlHttp.responseText;
			} catch(e) {
				// 2. can't do the innerHTML so look for a generic object "myResponseText"
				try {
					/* -- removing fades
					if(ge('myResponseText')) {
						changeOpac(0, 'myResponseText');	// set opacity to 0
					}
					*/
					ge('myResponseText').innerHTML = xmlHttp.responseText;
				} catch(e) {
					// 3. find all the forms, create the generic object "myResponseText" after the last form (assuming this is where the info should go)
					//location.href = tmpURL;
					//window.open(url,'pop','width=500,height=400');
					//var myForms = document.getElementsByTagName('form');
					var myDiv = document.createElement('div');
					myDiv.id = 'myResponseText';
					ge('breadCrumbs').parentNode.insertBefore(myDiv,ge('breadCrumbs').nextSibling);
					//myForms[myForms.length-1].parentNode.insertBefore(myDiv,myForms[myForms.length-1].nextSibling);
					myDiv.innerHTML = xmlHttp.responseText;
					myDiv.style.border = '1px solid #EEE';
					myDiv.style.width = '99%';
				}
			}
			/* -- removing fades
			try {
				opacity(obj.id, 0, 99, MYSPEED);
			} catch(e) {}
			
			try {
				if(ge('myResponseText')) {
					opacity('myResponseText', 0, 99, MYSPEED);
				}
			} catch(e) {}
			*/
			
			// remove the loading div if it's found
			if(ge('loading')) {
				ge('loading').parentNode.removeChild(ge('loading'));
			}
			// move page message from obj to parent message
			moveUserMsg(obj);
			// re-initialize the page (stripe table, sort table, etc.)
			init();
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


// gets form name/vals from given object (obj) and creates a querystring for ajax push pull function
function formVals(obj) {
	var str = '';
	// try and grab all the form elements in this object, can't assume we're in a form here
	
	// INPUTS
	var arrInputs = obj.getElementsByTagName('input');
	for (var i=0;i<arrInputs.length;i++) {
		if(arrInputs[i].name != '') {
			switch (arrInputs[i].type) {
				case 'radio':
				case 'checkbox':
					if(arrInputs[i].checked) {
						str = str + '&' + arrInputs[i].name + '=' + encodeURIComponent(arrInputs[i].value);
					}
					break;
				case 'button':
				case 'submit':
				case 'reset':
					break;
				default:
					str = str + '&' + arrInputs[i].name + '=' + encodeURIComponent(arrInputs[i].value);
			}
			
		}
	}
	// SELECTS
	var arrSelects = obj.getElementsByTagName('select');
	for (var i=0;i<arrSelects.length;i++) {
		if(arrSelects[i].name != '') {
			str = str + '&' + arrSelects[i].name + '=' + encodeURIComponent(arrSelects[i].value);
		}
	}
	// TEXTAREA
	var arrTxt = obj.getElementsByTagName('textarea');
	for (var i=0;i<arrTxt.length;i++) {
		if(arrTxt[i].name != '') {
			str = str + '&' + arrTxt[i].name + '=' + encodeURIComponent(arrTxt[i].value);
		}
	}
		
	return str;
}

// http://brainerror.net/scripts_js_blendtrans.php
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(var i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(var i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	if(ge(id)) {
		var object = ge(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
}

// for forms that need to submit w/ POST
/*
EXAMPLE:
<iframe name="post_iframe" id="post_iframe" src="about:blank" 
		style="display: none; width: 100%; height: 300px;"
		onload="ajaxPostPut(this,targetObj);"></iframe>
*/
function ajaxPostPut(iFrm,obj) {	
	// IE & Firefox
	if(iFrm.contentWindow) {
		if(iFrm.contentWindow.document.body.innerHTML != '') {
			obj.innerHTML = iFrm.contentWindow.document.body.innerHTML;
			iFrm.contentWindow.document.body.innerHTML = '';
			moveUserMsg(obj);
			/* -- removing fades
			// call fade in...
			opacity(obj.id, 0, 100, MYSPEED);
			*/
			try {
				init();
			} catch(e) {}
			return;
		}
	}
}
// move page message from returned HTML into userMessage if it exists
function moveUserMsg(o) {
	//alert(o.id);
	if(o.id == undefined) {
		return;
	}
	var myDivs = o.getElementsByTagName('div');
	var tmpMsg = '';
	var tmpCls = '';
	
	for(var i=0;i<myDivs.length;i++) {
		if(myDivs[i].id == 'userMessage') {
			// copy the message and class to the original userMessage element
			tmpMsg = myDivs[i].innerHTML;
			tmpCls = myDivs[i].className;
			// remove the message
			myDivs[i].parentNode.removeChild(myDivs[i]);
			
			if(ge('userMessage')) {
				ge('userMessage').innerHTML = tmpMsg;
				ge('userMessage').className = tmpCls;
				// move the message to the top-left corner
				floatMsg(ge('userMessage'));
				// call fade in...
				opacity('userMessage', 0, 100, MYSPEED);
				var timer = window.setTimeout("hideUserMsg()",10*1000);
				usermessage_updated_fl = true;
			}
			break;
		}
	}
}

function hideUserMsg() {
	if(ge('userMessage')) {
		// fade out the message
		opacity('userMessage', 100, 0, MYSPEED);
		// move the message back to its place
		var timer = window.setTimeout("fixMsg()",0.5*1000);
	}
}
// moves the message to the top-left corner
function floatMsg(msg){
	//msg.style.position = 'fixed';
	//msg.style.top = '1px';
	//msg.style.left = '1px';
	//msg.style.width = '500px';
	//msg.style.backgroundColor = '#FFF';
	msg.style.paddingRight = '10px';
	msg.style.zIndex = 100;
	if(document.all) {
		//alert(window.screenTop);
		msg.style.position = 'absolute';
		msg.style.top = Math.max(document.documentElement.scrollTop,document.body.scrollTop) + 'px';
		//window.status = msg.style.top + 'px';
	}
}
// moves the message back to default location
function fixMsg(){
	var msg = ge('userMessage');
	//msg.style.position = 'static';
	msg.style.paddingRight = '0px';
	msg.className = 'noMessage';
	//msg.style.width = 'auto';
	opacity(msg.id, 0, 100, MYSPEED);
}
// just in case it's not defined
function ge(o) {
	return document.getElementById(o);
}